package cn.myfirefly.moming.portal.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.myfirefly.moming.common.dao.RoleDataAuthDao;
import cn.myfirefly.moming.common.dao.UserDao;
import cn.myfirefly.moming.common.entity.MMUser;
import cn.myfirefly.moming.common.model.RoleDataAuthDto;
import cn.myfirefly.moming.portal.dao.FunctionActionDao;
import cn.myfirefly.moming.portal.model.FunctionActionDto;
import cn.myfirefly.core.Const;
import cn.myfirefly.core.model.DataAuth;
import cn.myfirefly.core.model.LoginDto;
import cn.myfirefly.core.model.Permission;
import cn.myfirefly.utils.BeanUtil;
import cn.myfirefly.utils.CollectionUtil;
import cn.myfirefly.utils.EncryptUtil;
import cn.myfirefly.utils.StrUtil;

@Service
@Transactional
public class LoginService {

	@Autowired
	private FunctionActionDao functionActionDao;
	
	@Autowired
	private UserDao userDao;
	
	@Autowired
	private RoleDataAuthDao roleDataAuthDao;
	
	public Map<String, Permission> getPermissions(String loginName) {
		List<FunctionActionDto> list = functionActionDao.getByLoginName(loginName);
		Map<String, Permission> permissions = new HashMap<String, Permission>();
		Permission permission = null;
		for (FunctionActionDto functionActionDto : list) {
			permission = permissions.get(functionActionDto.getFunId());
			if (permission == null) {
				permission = new Permission();
			}
			permission.setFunId(functionActionDto.getFunId());
			permission.addAction(functionActionDto.getAction());
			permissions.put(functionActionDto.getFunId(), permission);
		}
		return permissions;
	}
	
	public Map<String, Permission> getPermissions(Integer systemId, String loginName) {
		List<FunctionActionDto> list = functionActionDao.getByLoginName(systemId, loginName);
		Map<String, Permission> permissions = new HashMap<String, Permission>();
		Permission permission = null;
		for (FunctionActionDto functionActionDto : list) {
			permission = permissions.get(functionActionDto.getFunId());
			if (permission == null) {
				permission = new Permission();
			}
			permission.setFunId(functionActionDto.getFunId());
			permission.addAction(functionActionDto.getAction());
			permissions.put(functionActionDto.getFunId(), permission);
		}
		// 取得所有数据权限
		List<RoleDataAuthDto> dataList = roleDataAuthDao.getRoleDataAuthList(systemId, loginName);
		if (CollectionUtil.isNotEmpty(dataList)) {
			for (RoleDataAuthDto roleDataAuthDto : dataList) {
				permission = permissions.get(roleDataAuthDto.getFunId());
				if (permission != null) {
					if (permission.getDataAuth() == null) {
						DataAuth dataAuth = BeanUtil.copyBeans(DataAuth.class, roleDataAuthDto);
						permission.setDataAuth(dataAuth);
					}
				}
			}
		}
		return permissions;
	}
	
	public String savePwd(LoginDto dto) {
		if (!StrUtil.equals(dto.getNewPassword(), dto.getConfirmPassword())) {
			return "新密码与确认新密码不一致！";
		}
		
		if (StrUtil.equals(dto.getNewPassword(), dto.getPassword())) {
			return "新密码与原密码不能相同，请修改后再试！";
		}
		
		MMUser user = userDao.getUserByLoginName(dto.getLoginName());
		if (user == null) {
			return "用户不存在！";
		}
		
		// 判断原密码是否正确
		if (!StrUtil.equals(EncryptUtil.md5(dto.getPassword()), 
				user.getPassword())) {
			return "原密码不正确！";
		}
		
		user.setPassword(EncryptUtil.md5(dto.getNewPassword()));
		userDao.save(user);
		return Const.ResultCode.SUCCESS;
	}
	
	public String firstSavePwd(LoginDto dto) {
		if (!StrUtil.equals(dto.getNewPassword(), dto.getConfirmPassword())) {
			return "新密码与确认新密码不一致！";
		}
		
		MMUser user = userDao.getUserByLoginName(dto.getLoginName());
		if (user == null) {
			return "用户不存在！";
		}
		
		user.setPassword(EncryptUtil.md5(dto.getNewPassword()));
		user.setChangePass('0');
		userDao.save(user);
		return Const.ResultCode.SUCCESS;
	}
}
